%% This code reproduces Figure 3
% Parameters
afft.sigma = 5; % Elasticity of substitution
afft.xi_u = 1; % Exporting Potential of USA to North
afft.f_x_u = 0.1; % Plant-level FC of exporting for USA
afft.f_x_s = 0.2; % Plant-level FC of exporting for South (including assembly)
afft.max_xi_s = 5; % South exporting potential without trade costs

afft.theta = 1; % Cannibalization
afft.f_x = 2; % Fixed Costs of exporting
afft.tau_s_nu = 0.5; % Sourcing trade costs from North to USA
afft.xi_n_s = 2; % Sourcing potential of North
afft.tau_s_ns = 1; % Sourcing trade costs from North to South
afft.tau_s_us = 0.5; % Sourcing trade costs between USA and South
afft.tau_s_su = 0.5; % Sourcing trade costs between USA and South
afft.f_s = 7; % Sourcing fixed costs

% Figure 3
close all
graph_figure3(afft)

%% Functions
function fun = graph_figure3(afft)
afft.case5_solution = [transpose([0:0.01:1]), profit_solution_case5(afft, 0:0.01:1)];
[~,~,afft.case5_solution(:,4)] = unique(afft.case5_solution(:,2:3),'rows'); 
afft.exports_case5 = export_case5(afft, afft.case5_solution(:,1), afft.case5_solution(:,2:3));
afft.imports_case5 = import_case5(afft, afft.case5_solution(:,1), afft.case5_solution(:,2:3));
% Find values where the discrete change in exporting happens
temp = (afft.case5_solution(1:end-1,4)~=afft.case5_solution(2:end,4));
a = [0;temp];
temp = [temp;0];
afft.case5_jump = [afft.case5_solution(temp==1,:), afft.exports_case5(temp==1,:), afft.exports_case5(a==1,:)];

temp = unique(afft.case5_solution(:,4),'stable');

%%%% Graph for Exporting
fun = figure('Name','Exports')
a = (afft.case5_solution(:,4) == temp(1));
plot(afft.case5_solution(a,1),afft.exports_case5(a),'-','LineWidth', 4,'MarkerSize',16,'color',[0.1294    0.2196    0.6706]);
hold on
for i=2:size(temp,1)
    a = (afft.case5_solution(:,4) == temp(i));
    plot(afft.case5_solution(a,1),afft.exports_case5(a),'-','LineWidth', 4,'MarkerSize',16,'color',[0.1294    0.2196    0.6706]);
end
for i=1:size(afft.case5_jump,1)
    plot([afft.case5_jump(i,1) afft.case5_jump(i,1)],[afft.case5_jump(i,6) afft.case5_jump(i,5)],...
        '--','LineWidth', 4,'MarkerSize',16,'color',[0.1294    0.2196    0.6706])
end
xlim([0 1])
annotation('textarrow', [0.58 0.58], [0.4 0.5],...
    'String','\begin{tabular}{c}Activate Assembly \\ in South \end{tabular}', 'Interpreter','latex','fontsize',18)
annotation('textarrow', [0.75 0.75], [0.8 0.9],...
    'String','\begin{tabular}{c}Activate Sourcing \\ from North \end{tabular}', 'Interpreter','latex','fontsize',18)
xlabel('Inverse Trade Costs between South and North, $(\tau^a_{sn})^{1-\sigma}$','Interpreter','latex','fontsize',14)
ylabel('Exports from USA to North','Interpreter','latex','fontsize',14)
box off
set(gca, ...
    'TickDir'     , 'out'     , ...
    'TickLength'  , [.02 .02] , ...
    'YGrid'       , 'on'      , ...
    'XColor'      , [.3 .3 .3], ...
    'YColor'      , [.3 .3 .3], ...
    'Fontsize'    , 14        , ...
    'Linewidth'   , 1         );

%%% Uncomment it to get the Graph for Importing
%{
fun = figure('Name','Imports')
a = (afft.imports_case5==0);
b = (afft.imports_case5 > 0);
plot(afft.case5_solution(a,1),afft.imports_case5(a),'-','LineWidth', 4,'MarkerSize',16,'color',[0.1294    0.2196    0.6706]);
hold on
plot(afft.case5_solution(b,1),afft.imports_case5(b),'-','LineWidth', 4,'MarkerSize',16,'color',[0.1294    0.2196    0.6706]);
plot([0.6 0.6],[0 4],...
        '--','LineWidth', 4,'MarkerSize',16,'color',[0.1294    0.2196    0.6706])
xlim([0 1])
ylim([0 10])
annotation('textarrow', [0.75 0.75], [0.55 0.45],...
    'String','\begin{tabular}{c}Activate Sourcing \\ from North \end{tabular}', 'Interpreter','latex','fontsize',18)
xlabel('Inverse Trade Costs between South and North, $(\tau^a_{sn})^{1-\sigma}$','Interpreter','latex','fontsize',14)
ylabel('Imports from North to USA','Interpreter','latex','fontsize',14)
box off
set(gca, ...
    'TickDir'     , 'out'     , ...
    'TickLength'  , [.02 .02] , ...
    'YGrid'       , 'on'      , ...
    'XColor'      , [.3 .3 .3], ...
    'YColor'      , [.3 .3 .3], ...
    'Fontsize'    , 14        , ...
    'Linewidth'   , 1         );
%}

end


function fun=profit_solution_case5(afft, tau_s)
I = [0,0;1,0;0,1;1,1];
prof = I(:,2).*((1+I(:,1).*afft.xi_n_s.*afft.tau_s_nu).*...
    afft.xi_u+(afft.tau_s_us+I(:,1).*afft.xi_n_s.*afft.tau_s_ns).*afft.max_xi_s.*tau_s).^(afft.theta)...
    - I(:,1).*afft.f_s - I(:,2).*afft.f_x;
[~,a] = max(prof);
fun = I(a,:);
end

function fun=export_case5(afft, tau_s, I)
export = afft.sigma.*I(:,2).*((1+I(:,1).*afft.xi_n_s.*afft.tau_s_nu).*afft.xi_u).*...
    ((1+I(:,1).*afft.xi_n_s.*afft.tau_s_nu).*afft.xi_u+...
    (afft.tau_s_us+I(:,1).*afft.xi_n_s.*afft.tau_s_ns).*afft.max_xi_s.*tau_s).^(afft.theta-1);
fun = export;
end

function fun=import_case5(afft, tau_s, I)
import = (1-1./afft.sigma).*export_case5(afft, tau_s, I).*I(:,1).*afft.xi_n_s.*afft.tau_s_nu./(1+I(:,1).*afft.xi_n_s.*afft.tau_s_nu);
fun = import;
end